package com.second.ssyt_test.common.util;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.Validate;
import org.apache.log4j.Logger;

import com.second.ssyt_test.common.util.AppUtil;
import com.second.ssyt_test.common.util.Util;
import com.second.ssyt_test.common.Constant;
import com.second.ssyt_test.role.entity.SysRoleEntity;

/**
 * ��ǰ��Ŀ�Ĺ��߷���
 *
 * @author Hsu.
 * @since 2016��1��19��
 */
public class AppUtil {

	private static final String DEFAULT_PROPERTIES_NAME = "jdbc";
	private static Logger LOG = Logger.getLogger(AppUtil.class);

	private AppUtil() {

	}

	/**
	 * ��ȡproperties�ļ�������
	 * 
	 * @param key
	 * @return
	 */
	public static String getDefaultBundle(String key) {
		return getBundle(DEFAULT_PROPERTIES_NAME, key);
	}

	public static String getBundle(String properitesName, String key) {
		Validate.notBlank(properitesName,
				"Properties File Name" + Constant.VALIDATE_NOT_BLANK);
		Validate.notBlank(key, "Properties Key" + Constant.VALIDATE_NOT_BLANK);

		return ResourceBundle.getBundle(properitesName).getString(key);
	}
	
	public static SysRoleEntity getSysRole(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute("talent") == null) {
			request.setAttribute("sessionClosed", "会话已经关闭！");
			try {
				request.getRequestDispatcher("")
						.forward(request, response);
			} catch (ServletException | IOException e) {
				Util.e(LOG, e.getMessage(), e);
			}
			throw new RuntimeException("会话已经关闭！获取Session中的内容失败");
		} else {
			return (SysRoleEntity) request.getSession().getAttribute("sysRole");
		}
	}

	public static int getSysRoleId(HttpServletRequest request,
			HttpServletResponse response) {
		return getSysRole(request, response).getId();
	}

}